package com.tanhua.autoconfig.templates;

import com.baidu.aip.face.AipFace;
import com.tanhua.autoconfig.propertirs.ApiFaceProperties;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;

public class ApiFaceTemplate {
    @Autowired
    private ApiFaceProperties apiFaceProperties;
    public Boolean detect(String image){
        //图片的url，base64编码或者图片的token
       // String image = "取决于image_type参数，传入BASE64字符串或URL字符串或FACE_TOKEN字符串";
        // 初始化一个AipFace
        AipFace client = new AipFace(apiFaceProperties.getAppId(), apiFaceProperties.getApiKey(), apiFaceProperties.getSecretKey());
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");
        //图片类型
        String imageType = "URL";

        // 人脸检测
        JSONObject res = client.detect(image, imageType, options);
        //返回值是map对象，err_code为0就包含人脸，不为0不包含人脸
        System.out.println(res.toString(2));
        int err_code = res.getInt("error_code");
        return err_code == 0; //=0就是返回true，否则返回false
    }
}
